<?php

namespace app\achievement\controller;

use app\common\model\AreasModel;
use app\common\model\CustomerLabelModel;
use app\common\model\CustomerModel;
use app\common\model\FollowModel;
use app\common\model\OaWorkersModel;
use app\common\model\FundsRecordModel;
use app\common\service\ExcelService;

/**
 * Class Test
 * @package app\achievement\controller
 */
class Employee extends Base
{
    // 初始化构造方法

    public function __construct()
    {
        parent::__construct();


    }

    // 员工列表（业务员管理）
    public function index()
    {
        //获取当前用户权限
        $is_admin = session($this->set['db_name'] . 'is_admin');
        if (!$is_admin) {
            $rule = session($this->set['db_name'] . 'rule');
            //限制划拨
            $tempMoneyTransfer = false;
            if (!empty($rule)) {
                $rule = explode(',', $rule);
                if (in_array('322', $rule)) {
                    $tempMoneyTransfer = true;
                }
            }
        } else {
            $tempMoneyTransfer = true;
        }

        $this->assign('tempMoneyTransfer', $tempMoneyTransfer);
        return $this->fetch();
    }

    // 员工编辑
    public function edit()
    {
        $id = input('id');
        $this->assign('id', $id);
        $is_admin = session($this->set['db_name'] . 'is_admin');
        if (!$is_admin) {
            //获取当前用户权限
            $rule = session($this->set['db_name'] . 'rule');
            //限制
            $editMoney = false;
            $edit = false;
            if (!empty($rule)) {
                $rule = explode(',', $rule);
                if (in_array('327', $rule)) {
                    $editMoney = true;
                }
                if (in_array('328', $rule)) {
                    $edit = true;
                }
            }
        } else {
            $editMoney = true;
            $edit = true;
        }

        $this->assign('editMoney', $editMoney);
        $this->assign('edit', $edit);
        // 调用视图
        return $this->fetch();
    }

    // 业务员账户变动记录
    public function fundsRecord()
    {
        $id = input('id');
        $this->assign('id', $id);
        // 调用视图
        return $this->fetch();
    }

    // 下属关系结构图
    public function structure()
    {
        $id = input('id');
        $this->assign('id', $id);
        return $this->fetch();
    }

    // 业务员申请列表
    public function apply()
    {
        $is_admin = session($this->set['db_name'] . 'is_admin');
        if (!$is_admin) {
            $rule = session($this->set['db_name'] . 'rule');
            //限制
            $examine = 0;
            if (!empty($rule)) {
                $rule = explode(',', $rule);
                if (in_array('308', $rule)) {
                    $examine = 1;
                }
            }
        } else {
            $examine = 1;
        }
        $this->assign('examine', $examine);
        return $this->fetch();
    }

    //开会奖惩记录
    public function meeting()
    {
        return $this->fetch();
    }

    //新增开会奖惩记录
    public function add_meeting()
    {
        return $this->fetch();
    }

    // 导入名单
    public function importMember()
    {
        $file = $this->request->file('file');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/meeting_member');
            if ($info) {
                $file_path = ROOT_PATH . 'public' . DS . 'uploads/meeting_member' . DS . $info->getSaveName();
                if (!file_exists($file_path)) {
                    returnJsonAdmin(-1003, '文件不存在，请重新上传');
                }
                $array = ExcelService::readExcel($file_path);
                unset($array[1]);
                unset($array[2]);
                // 操作数据
                if (empty($array)) {
                    returnJsonAdmin(-1003, '导入数据为空');
                }
                $arr = [];
                foreach ($array as $key => &$value) {
                    if (empty($value['B']) || empty($value['C'])) {
                        continue;
                    }
                    $item['account'] = $value['B'];
                    $item['telphone'] = $value['C'];
                    $arr[] = $item;
                }
                returnJson(1000,"上传成功",$arr);
            } else {
                // 上传失败获取错误信息
                returnJson(-1003, '上传文件失败');
            }
        }
    }

    //业绩考核
    public function inspect(){
        return $this->fetch();
    }


}
